package yanjunwei.rbac.view;


import yanjunwei.rbac.domain.dto.DeptDto;
import yanjunwei.rbac.domain.vo.DeptVo;
import yanjunwei.rbac.domain.vo.Result;
import yanjunwei.rbac.service.IDeptService;
import yanjunwei.rbac.service.impl.DeptService;
import yanjunwei.rbac.util.DateTimUtils;
import yanjunwei.rbac.util.SingletonFactory;


public class DeptView extends BaseView<DeptVo,IDeptService>{
    public static final String MENU = """
            --------------------
                 用户管理
            --------------------
            |  0...返回上级     |
            |  1...用户列表     |
            |  2...创建用户     |
            |  3...更新用户     |
            |  4...切换状态     |
            --------------------
            请输入选项:""";

 
    private DeptView() {
        service = SingletonFactory.getInstance(DeptService.class);
    }




    public void show() {
        String menuChoice;
        do {
            System.out.print(MENU);
            menuChoice = MainView.input.nextLine();

            switch (menuChoice) {
                case "0" -> System.out.println("已返回上一级菜单！");
                case "1" -> list();
                case "2" -> create();
                case "3" -> update();
                case "4" -> delete();
                default -> System.out.println("输入非法，请输入正确的菜单编号！");
            }
        } while (!"0".equals(menuChoice));
    }



    public void list() {
        Result result = service.list();
        cache = (DeptVo[]) result.data;
        System.out.println("Id\t部门名称\t部门电话\t更新时间");
        for (DeptVo dept : cache) {
            if (dept != null) {
                System.out.printf("%d\t%s\t%s\t%s\n", dept.id, dept.name, dept.phone, DateTimUtils.format(dept.updateAt));
            }
        }
    }


    public void create() {
        System.out.print("请输入部门名称:");
        String name = MainView.input.nextLine();

        System.out.print("请输入部门电话:");
        String phone = MainView.input.nextLine();
        DeptDto dto = new DeptDto(name, phone);
        Result result = service.create(dto);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public void update() {

        DeptVo dept = getCacheById();
        if (dept == null) {
            return;
        }


        System.out.print("请输入部门名称,直接回车表示不修改:");
        String name = MainView.input.nextLine();
        name = "".equals(name) ? dept.name : name;


        System.out.print("请输入部门电话,直接回车表示不修改:");
        String phone = MainView.input.nextLine();
        phone = "".equals(phone) ? dept.phone : phone;

        DeptDto dto = new DeptDto(name, phone);
        Result result = service.update(dto, dept.id);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    private void delete() {
        DeptVo dept = getCacheById();
        if (dept == null) {
            return;
        }
        System.out.print("输入'Y确认删除，输入其他取消删除:");
        String confirm = MainView.input.nextLine();
        if (!"Y".equalsIgnoreCase(confirm)) {
            System.out.println("您已取消删除");
            return;
        }
        Result result = service.delete(dept.id);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }



}

